package com.rex.saas.service;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Arrays;

/**
 * @ClassName AssetService
 * @Description:
 * @Author: zhusiyu
 * @CreateDate: 2024/12/18 11:06	//创建时间
 */
@Component
@Slf4j
public class AiService {
    private static String ApiKey="sk-fa55dc7e245f46a28a5fbd6dc7361982";//这里填写自己申请的APIKEY

    @PostMapping("/t2t")
    public GenerationResult t2t(String str) throws NoApiKeyException, ApiException, InputRequiredException {
        Generation gen = new Generation();
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(str).build();//这里填写对话内容
        GenerationParam param = GenerationParam.builder().model("qwen-plus")
                .apiKey(Constants.apiKey)
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE).topP(0.8).enableSearch(true)
                .build();
        return gen.call(param);
    }

    @PostMapping("/t2t2")
    public ApplicationResult t2t2(String str) throws NoApiKeyException, ApiException, InputRequiredException {
        ApplicationParam applicationParam = ApplicationParam.builder()
                .appId("7da8e47a339b4bfca274304da0698ff1")
                .apiKey(Constants.apiKey)
                .prompt(str)
                .apiKey(ApiKey)
                .build();
        Application application = new Application();
        return application.call(applicationParam);
    }
}
